package com.meichis.yndms.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.activity.TakePhotoActivity;
import com.google.android.entity.CameraParam;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.ParseUtil;
import com.meichis.mcsappframework.utils.UniversalInterface;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.ImageAdapter;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.model.Picture;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shizw on 2018/4/4.
 */

public class CameraView extends LinearLayout implements View.OnClickListener{

    private View view;
    private ImageButton takePhoto;
    private Context context;
    private RecyclerView recyclerView;
    private List<Picture> pictures = new ArrayList<>();
    //最大存储图片数量
    private static int PICTURE_MAX_COUNT = 6;
    private ImageAdapter imageAdapter;
    private static boolean IsNeedChooseFromAlbum = false;
    public static final int CAMERA_IMAGE = 2;
    public int position = 0;
    private String waterParams;
    private OnItemDeleteListener listener;

    private ArrayList<InspectTemplate_Item> items;

    public  void setTakePhotoVisity(){
        takePhoto.setVisibility(View.GONE);
    }

    public CameraView(Context context) {
        super(context);
        this.context = context;
    }

    public CameraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
        initEvent();
    }

    private void initView()
    {
        view = LayoutInflater.from(context).inflate(R.layout.camera_view,this);
        takePhoto = view.findViewById(R.id.ibt_takephoto);
        recyclerView = view.findViewById(R.id.RV_photoList);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        imageAdapter = new ImageAdapter(context, R.layout.activity_signin_photoitem, pictures);
        recyclerView.setAdapter(imageAdapter);
    }

    private void initEvent()
    {
        takePhoto.setOnClickListener(this);
        imageAdapter.setonDeleteListener(new UniversalInterface<Void, Integer>() {
            @Override
            public Void todo(Integer integer) {
                if(listener != null){
                    listener.OnItemDelete(integer);
                }
                return null;
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.ibt_takephoto:
                if((ParseUtil.parseInt(items.get(position).getItem().getMaxValue()) >0 && this.pictures.size() < ParseUtil.parseInt(items.get(position).getItem().getMaxValue()))
                        || (ParseUtil.parseInt(items.get(position).getItem().getMaxValue()) == 0 && this.pictures.size() <= 6))
                {
                    final Intent intent = new Intent(context, TakePhotoActivity.class);
                    intent.putExtra("position", position);
                    intent.putExtra("IsNeedChooseFromAlbum",IsNeedChooseFromAlbum);
                    final CameraParam takePhotoParam = new CameraParam();
                    //设置拍照水印
                    if(!TextUtils.isEmpty(waterParams))
                    {
                        String[] str = waterParams.split("\n");
                        String tempWater = "";
                        for(String s : str){
                            if(s.startsWith("拍摄时间")){
                                tempWater = tempWater + DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS) + "\n";
                            }else {
                                tempWater = tempWater + s + "\n";
                            }
                        }
                        takePhotoParam.setWatermark(tempWater);
                    }
                    new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT).setMessage("请选择照片品质").setPositiveButton("高清", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            takePhotoParam.setPicHight(1280);
                            takePhotoParam.setPicQuality(60);
                            intent.putExtra("CameraParam", takePhotoParam);
                            ((Activity)context).startActivityForResult(intent, CameraView.CAMERA_IMAGE);
                        }
                    }).setNegativeButton("普通", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            takePhotoParam.setPicHight(800);
                            takePhotoParam.setPicQuality(40);
                            intent.putExtra("CameraParam", takePhotoParam);
                            ((Activity)context).startActivityForResult(intent, CameraView.CAMERA_IMAGE);
                        }
                    }).show();
                }else{
                    MessageTools.showLongToast("图片已达最大数量");
                }
                break;
        }
    }

    /**
     * 填充图片
     * @param list
     */
    public void addPictures(List<Picture> list)
    {
        this.pictures.addAll(list);
        this.imageAdapter.notifyDataSetChanged();
    }

    /**
     * 填充图片
     * @param list
     */
    public void setPictures(List<Picture> list)
    {
        this.pictures.clear();
        this.pictures.addAll(list);
        this.imageAdapter.notifyDataSetChanged();
    }


    /**
     * 设置图片的最大数量
     * 默认最大数量为3
     * @param maxCount 必须大于0
     */
    public void setPictureMaxCount(int maxCount)
    {
        if(maxCount <= 0)
        {
            MessageTools.showShortToast("请输入有效数值");
        }else{
            this.PICTURE_MAX_COUNT = maxCount;
        }
    }

    /**
     * 设置是否需要从相册选择
     * @param isNeed
     */
    public void setIsNeedChooseFromAlbum(boolean isNeed)
    {
        this.IsNeedChooseFromAlbum = isNeed;
    }

    /**
     * 获取当前图片数据
     * @return
     */
    public List<Picture> getPictures()
    {
        return pictures;
    }

    /**
     * 设置照片水印
     * @param waterParams
     */
    public void setWaterParams(String waterParams)
    {
        this.waterParams = waterParams;
    }

    /**
     * recyclerView需要
     * 一般用不到
     * @param position
     */
    public void setPosition(int position)
    {
        this.position = position;
    }

    /**
     * recyclerView需要
     * 一般用不到
     * @return
     */
    public int getPosition()
    {
        return position;
    }

    public void setOnPictureDelete(OnItemDeleteListener listener){
        this.listener = listener;
    }

    public interface OnItemDeleteListener{
        void OnItemDelete(int integer);
    }

    /**
     * 刷新adapter
     */
    public void notifyDataSetChanged(){
        this.imageAdapter.notifyDataSetChanged();
    }
    /**
     * 设置图片数量大于一定值是隐藏拍照按钮
     */
    public void setVisibility(){
        if (pictures.size() >= PICTURE_MAX_COUNT) {
            takePhoto.setVisibility(View.GONE);
        }else{
            takePhoto.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 返回ImageAdapter
     * 方便取适配器中的新数据
     * @return
     */
    public ImageAdapter getImageAdapter()
    {
        return imageAdapter;
    }

    public void setDatas(ArrayList<InspectTemplate_Item> items){
        if(this.items == null){
            this.items = new ArrayList<>();
        }
        this.items.clear();
        this.items.addAll(items);
    }
}
